﻿$(function() {
    $('#dlg-cash').dialog('close');
    $('#dlg-additem').dialog('close');
});


function operFormatter(value, rec, rowIndex) {
    if (rec.ItemCode) {
        var ret = '<a value="' + rowIndex + '" type=0 class="easyui-linkbutton l-btn" href="#"><span class="l-btn-left"><span class="l-btn-text">明细</span></span></a>';
        if (parseInt(rec.ItemType) == 2)
            ret += '<a value="' + rowIndex + '" type=1 class="easyui-linkbutton l-btn" href="#"><span class="l-btn-left"><span class="l-btn-text">删除</span></span></a>';
        return ret;
    } else {
        return '';
    }
}


function changeTxtFeeValue() {
    var rows = $('#srccitemGird').datagrid('getSelections');
    var total = 0;
    var favour = 0;
    var reality = 0;
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            total += parseFloat(rows[i].ThisCostFee) + parseFloat(rows[i].ThisLateFee) - parseFloat(rows[i].Lastsurplus) - parseFloat(rows[i].LastLateFee);
            favour += parseFloat(rows[i].FavourFee);
            reality += parseFloat(rows[i].RealityFee);
        }
    }
    $('#txtTotalFee').numberbox('setValue', total);
    $('#txtFavourFee').numberbox('setValue', favour);
    $('#txtRealityFee').numberbox('setValue', reality);

    var val = parseFloat($('#txtcash').val()) - parseFloat($('#txtRealityFee').val());
    $('#txtchange').numberbox('setValue', val);
}

function inputdisable_dlg_additem(type) {
    $("#dlg_additem_FavourFee").unbind("change");
    //$("#dlg_additem_FavourFee").unbind("blur");
    $("#dlg_additem_ThisCostFee").unbind("change");
    $("#dlg_additem_RealityFee").unbind("change");

    if (parseInt(type) == 0) {//时间型
        $('#dlg_additem_ItemName').combobox('disable');
        $('#dlg_additem_Price').numberbox('disable');
        $('#dlg_additem_ThisNum').numberbox('disable');
        $('#dlg_additem_Lastmaturity').datebox('disable');
        $('#dlg_additem_Thismaturity').datebox('enable'); //调整本次到期日
        $('#dlg_additem_ThisCostFee').numberbox('disable');
        $('#dlg_additem_ThisLateFee').numberbox('disable');
        $('#dlg_additem_FavourFee').numberbox('enable'); //调整优惠金额
        $('#dlg_additem_RealityFee').numberbox('disable');
        $('#dlg_additem_PrepayFee').numberbox('disable'); //预交金额

        $('#dlg_additem_Thismaturity').datebox('options').onSelect = function(date) {//修改本次到期日，后台计算应收金额、滞纳金和费用头
            var datevalue = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            var params = { SrccId: $('#dlg_additem_SrccId').val(), ItemCode: $('#dlg_additem_ItemCode').val(), Thismaturity: datevalue };
            requestAjax('../Controller.aspx?controller=RoomBrowseController&method=ChangeThismaturity', params, function(data) {
                $('#dlg_additem_ThisCostFee').numberbox('setValue', data.ThisCostFee);
                $('#dlg_additem_ThisLateFee').numberbox('setValue', data.ThisLateFee);

                var realityfee = parseFloat($('#dlg_additem_ThisCostFee').val()) + parseFloat($('#dlg_additem_ThisLateFee').val()) - parseFloat($('#dlg_additem_Lastsurplus').val()) - parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat($('#dlg_additem_FavourFee').val());
                $('#dlg_additem_RealityFee').numberbox('setValue', realityfee);

                $('#itemheadlistGird').datagrid('loadData', data.itemheadlistGird);
            });
        }

        $("#dlg_additem_FavourFee").bind("change", function() {
            var realityfee = parseFloat($('#dlg_additem_ThisCostFee').val()) + parseFloat($('#dlg_additem_ThisLateFee').val()) - parseFloat($('#dlg_additem_Lastsurplus').val()) - parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat($(this).val());
            $('#dlg_additem_RealityFee').numberbox('setValue', realityfee);
        });

        //    $("#dlg_additem_FavourFee").bind("blur", function() {
        //        var decvalue = parseFloat($("#dlg_additem_FavourFee").val()) - parseFloat($('#dlg_additem_ThisCostFee').val()) - parseFloat($('#dlg_additem_ThisLateFee').val());
        //        if (decvalue > 0) {
        //            $.messager.alert('提示', "优惠金额不能大于应收金额与滞纳金之和！", 'info', function() {
        //                $('#dlg_additem_FavourFee').numberbox('setValue', 0);
        //                var realityfee = parseFloat($('#dlg_additem_ThisCostFee').val()) + parseFloat($('#dlg_additem_ThisLateFee').val()) - parseFloat($('#dlg_additem_Lastsurplus').val()) - parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat(0);
        //                $('#dlg_additem_RealityFee').numberbox('setValue', realityfee);
        //                $('#dlg_additem_FavourFee').focus().select();
        //            });
        //        }
        //    });

    } else if (parseInt(type) == 1) {//用表型
        $('#dlg_additem_ItemName').combobox('disable');
        $('#dlg_additem_Price').numberbox('disable');
        $('#dlg_additem_ThisNum').numberbox('disable');
        $('#dlg_additem_Lastmaturity').datebox('disable');
        $('#dlg_additem_Thismaturity').datebox('disable');
        $('#dlg_additem_ThisCostFee').numberbox('disable');
        $('#dlg_additem_ThisLateFee').numberbox('disable');
        $('#dlg_additem_FavourFee').numberbox('enable'); //调整优惠金额
        $('#dlg_additem_RealityFee').numberbox('enable'); //调整实收金额 如果实收>应收 则有余额
        $('#dlg_additem_PrepayFee').numberbox('disable'); //预交金额

        $("#dlg_additem_FavourFee").bind("change", function() {
            var realityfee = parseFloat($('#dlg_additem_ThisCostFee').val()) + parseFloat($('#dlg_additem_ThisLateFee').val()) - parseFloat($('#dlg_additem_Lastsurplus').val()) - parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat($(this).val());
            if (realityfee >= 0) {
                $('#dlg_additem_RealityFee').numberbox('setValue', realityfee);
                $('#dlg_additem_PrepayFee').numberbox('setValue', 0);
            } else {
                $('#dlg_additem_RealityFee').numberbox('setValue', 0);
                //本次余额=实收金额+优惠金额+上次余额+上次滞纳金-应收金额-滞纳金
                var prepayFee = 0 + parseFloat($(this).val()) + parseFloat($('#dlg_additem_Lastsurplus').val()) + parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat($('#dlg_additem_ThisCostFee').val()) - parseFloat($('#dlg_additem_ThisLateFee').val());
                $('#dlg_additem_PrepayFee').numberbox('setValue', prepayFee);
            }
        });

        $("#dlg_additem_RealityFee").bind("change", function() {
            //本次余额=实收金额+优惠金额+上次余额+上次滞纳金-应收金额-滞纳金
            var prepayFee = parseFloat($(this).val()) + parseFloat($('#dlg_additem_FavourFee').val()) + parseFloat($('#dlg_additem_Lastsurplus').val()) + parseFloat($('#dlg_additem_LastLateFee').val()) - parseFloat($('#dlg_additem_ThisCostFee').val()) - parseFloat($('#dlg_additem_ThisLateFee').val());
            $('#dlg_additem_PrepayFee').numberbox('setValue', prepayFee);
        });

    }
    else {//新增 或者 其他型
        $('#dlg_additem_ItemName').combobox('enable'); //选择收费项目
        $('#dlg_additem_Price').numberbox('disable');
        $('#dlg_additem_ThisNum').numberbox('enable'); //修改数量
        $('#dlg_additem_Lastmaturity').datebox('disable');
        $('#dlg_additem_Thismaturity').datebox('disable');
        $('#dlg_additem_ThisCostFee').numberbox('enable'); //修改应收金额
        $('#dlg_additem_ThisLateFee').numberbox('disable');
        $('#dlg_additem_FavourFee').numberbox('disable');
        $('#dlg_additem_RealityFee').numberbox('disable'); //跟着应收金额变
        $('#dlg_additem_PrepayFee').numberbox('disable'); //预交金额


        $("#dlg_additem_ThisCostFee").bind("change", function() {
            $('#dlg_additem_RealityFee').numberbox('setValue', $(this).val());
        });

    }
}

function srccitemGrid_bindclick() {
    $('a[value]').unbind("click");
    $('a[value]').click(function() {//点击明细
        var val = $(this).attr('value');
        var type = $(this).attr('type');
        var row = $('#srccitemGird').datagrid('getData').rows[val];
        if (type == 0) {
            $('#dlg-additem').dialog({ title: '查看项目费用明细' });

            //加载下拉框所有项目数据
            $('#dlg_additem_ItemName').combobox({ url: '../Controller.aspx?controller=RoomBrowseController&method=GetCbServiceItemData&ItemType=' + row.ItemType });
            $('#dlg_additem_ItemName').combobox('options').onSelect = function(record) {
                $('#dlg_additem_ItemCode').val(record.Code);
                $('#dlg_additem_Price').numberbox('setValue', record.Price);
            }
            $('#dlg_additem_ItemType').val(row.ItemType);
            $('#dlg_additem_Lastsurplus').val(row.Lastsurplus);
            $('#dlg_additem_LastLateFee').val(row.LastLateFee);
            $('#dlg_additem_Index').val(val);

            $('#dlg_additem_HeadId').val(row.HeadId);

            $('#dlg_additem_ItemCode').val(row.ItemCode);
            $('#dlg_additem_ItemName').combobox('setValue', row.ItemCode);
            $('#dlg_additem_Price').numberbox('setValue', row.Price);
            $('#dlg_additem_ThisNum').numberbox('setValue', row.ThisNum);
            $('#dlg_additem_Lastmaturity').datebox('setValue', formatterStringToDate(row.Lastmaturity));
            $('#dlg_additem_Thismaturity').datebox('setValue', formatterStringToDate(row.Thismaturity));
            $('#dlg_additem_ThisCostFee').numberbox('setValue', row.ThisCostFee);
            $('#dlg_additem_ThisLateFee').numberbox('setValue', row.ThisLateFee);
            $('#dlg_additem_FavourFee').numberbox('setValue', row.FavourFee);
            $('#dlg_additem_RealityFee').numberbox('setValue', row.RealityFee);
            $('#dlg_additem_PrepayFee').numberbox('setValue', row.PrepayFee);
            $('#dlg_additem_memo').val(row.SrccItemMemo);

            $('#itemheadlistGird').datagrid('options').url = '../Controller.aspx?controller=RoomBrowseController&method=GetitemHeadData&HeadId=' + row.HeadId + '&srccId=' + row.SrccId + '&ItemCode=' + row.ItemCode;
            $('#itemheadlistGird').datagrid('reload');

            inputdisable_dlg_additem(row.ItemType);
        } else {
            if (parseInt(row.ItemType) == 2) {
                //只有新增的项目才有删除功能
                $.messager.confirm('询问', '是否真的删除此记录？', function(r) {
                    if (r) {
                        requestAjax('../Controller.aspx?controller=RoomBrowseController&method=DeleteItemFeeHead', { HeadId: row.HeadId }, function(data) {
                            //$('#srccitemGird').datagrid('deleteRow',val);
                            $('#srccitemGird').datagrid('reload');
                        });
                    }
                });
            }
        }
        //alert(val);
        return false;
    });
}

function cashmanager_show() {//显示收银界面
    var seleced = $('#floor').floor('getSelectedRoomData');
    if (seleced) {
        $('#dlg-cash').dialog('open');
        $('#cash_srccname').empty().append(seleced.srccName);
        $('#cash_usearea').empty().append(seleced.UseArea);
        $('#cash_ownername').empty().append(seleced.OwnerName);
        $('#cash_OftenMobile').empty().append(seleced.OftenMobile);
        $('#cash_CardNum').empty().append(seleced.CardNum);
        $('#dlg_additem_OwnerId').val(seleced.OwnerId);
        $('#dlg_additem_OwnerName').val(seleced.OwnerName);
        $('#dlg_additem_SrccId').val(seleced.srccId);
        $('#txtcash').numberbox('setValue', 0);

        $('#srccitemGird').datagrid('options').url = "../Controller.aspx?controller=RoomBrowseController&method=GetSrccitemData&srccId=" + seleced.srccId;
        $('#srccitemGird').datagrid('reload');

        $('#srccitemGird').datagrid('options').onLoadSuccess = function(data) {
            srccitemGrid_bindclick();
            $('#srccitemGird').datagrid('selectAll');
        }

        $('#srccitemGird').datagrid('options').onSelect = function(rowIndex, rowData) {
            changeTxtFeeValue();
        }

        $('#srccitemGird').datagrid('options').onSelectAll = function(rowIndex, rowData) {
            changeTxtFeeValue();
        }

        $('#srccitemGird').datagrid('options').onUnselect = function(rowIndex, rowData) {
            changeTxtFeeValue();
        }

        $('#srccitemGird').datagrid('options').onUnselectAll = function(rowIndex, rowData) {
            changeTxtFeeValue();
        }


        //输入现金金额，自动改变找零金额
        $('#txtcash').focus(function() { $('#txtcash').select() }).change(function() {
            var val = parseFloat($('#txtcash').val()) - parseFloat($('#txtRealityFee').val());
            $('#txtchange').numberbox('setValue', val);
        });

    }
}

function dlg_cashmanager_additemshow() {//点击添加项目
    $('#dlg-additem').dialog({ title: '添加项目' });


    //加载下拉框所有项目数据
    $('#dlg_additem_ItemName').combobox({ url: '../Controller.aspx?controller=RoomBrowseController&method=GetCbServiceItemData&ItemType=2' });
    $('#dlg_additem_ItemName').combobox('options').onSelect = function(record) {
        $('#dlg_additem_ItemCode').val(record.Code);
        $('#dlg_additem_Price').numberbox('setValue', record.Price);
    }
    inputdisable_dlg_additem(2);
    $('#dlg_additem_ItemType').val(2);
    $('#dlg_additem_Index').val(-1);
    $('#dlg_additem_HeadId').val("");

    var date = new Date();
    var datevalue = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

    $('#dlg_additem_ItemCode').val('');
    $('#dlg_additem_ItemName').combobox('setValue', '');
    $('#dlg_additem_Price').numberbox('setValue', 0);
    $('#dlg_additem_ThisNum').numberbox('setValue', 0);
    $('#dlg_additem_Lastmaturity').datebox('setValue', datevalue);
    $('#dlg_additem_Thismaturity').datebox('setValue', datevalue);
    $('#dlg_additem_ThisCostFee').numberbox('setValue', 0);
    $('#dlg_additem_ThisLateFee').numberbox('setValue', 0);
    $('#dlg_additem_FavourFee').numberbox('setValue', 0);
    $('#dlg_additem_RealityFee').numberbox('setValue', 0);
    $('#dlg_additem_PrepayFee').numberbox('setValue', 0);
    $('#dlg_additem_memo').empty();

    $('#itemheadlistGird').datagrid('loadData', emptyData);
}

function FeeStyler(value) {
    return 'color: #0000FF; text-align: right';
}

function formatterStringToDate(strdate) {
    var date = new Date(Date.parse(strdate.replace(/-/g, "/")));
    var datevalue = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    return datevalue;
}

function Dateformatter(value) {
    if (value) {
        return formatterStringToDate(value);
    } else {
        return '';
    }
}

function dlg_additem_queding() {//待完成 更新当前行
    if (formvalidate('dlg-additem') == false) return false;
    if (parseInt($('#dlg_additem_ItemType').val()) == 0) {
        //2
        var _lastdate = $('#dlg_additem_Lastmaturity').datebox('getValue');
        var lastdate = new Date(Date.parse(_lastdate.replace(/-/g, "/")));
        var _thisdate = $('#dlg_additem_Thismaturity').datebox('getValue');
        var thisdate = new Date(Date.parse(_thisdate.replace(/-/g, "/")));
        if (thisdate <= lastdate) {
            $.messager.alert('提示', "本次到期日不能小于等于上次到期日！", 'info');
            return false;
        }
        //1
        var decvalue = parseFloat($("#dlg_additem_FavourFee").val()) - parseFloat($('#dlg_additem_ThisCostFee').val()) - parseFloat($('#dlg_additem_ThisLateFee').val());
        if (decvalue > 0) {
            $.messager.alert('提示', "优惠金额不能大于应收金额与滞纳金之和！", 'info', function() {
                $('#dlg_additem_FavourFee').focus().select();
            });
            return false;
        }
        //时间型操作
        $.messager.confirm('询问', '是否需要进行此操作？', function(r) {
            if (r) {
                $.messager.progress({
		            title:'修改费用',
		            msg:'正在提交数据，请等待...'
	            });
                var params = { SrccId: $('#dlg_additem_SrccId').val(), ItemCode: $('#dlg_additem_ItemCode').val(), Thismaturity: $('#dlg_additem_Thismaturity').datebox('getValue'), ThisCostFee: $('#dlg_additem_ThisCostFee').val(), ThisLateFee: $('#dlg_additem_ThisLateFee').val(), FavourFee: $('#dlg_additem_FavourFee').val(), RealityFee: $('#dlg_additem_RealityFee').val() };
                requestAjax('../Controller.aspx?controller=RoomBrowseController&method=UpdateTimeSrccItemData', params, function(data) {
                    $('#dlg-additem').dialog('close');
                    $('#srccitemGird').datagrid('reload');
                    $.messager.progress('close');
                },function(){$.messager.progress('close');});
            }
        });

    } else if (parseInt($('#dlg_additem_ItemType').val()) == 1) {
        //1
        var decvalue = parseFloat($("#dlg_additem_FavourFee").val()) - parseFloat($('#dlg_additem_ThisCostFee').val()) - parseFloat($('#dlg_additem_ThisLateFee').val());
        if (decvalue > 0) {
            $.messager.alert('提示', "优惠金额不能大于应收金额与滞纳金之和！", 'info', function() {
                $('#dlg_additem_FavourFee').focus().select();
            });
            return false;
        }
        //用表型操作
        $.messager.confirm('询问', '是否需要进行此操作？', function(r) {
            if (r) {
                $.messager.progress({
		            title:'修改费用',
		            msg:'正在提交数据，请等待...'
	            });
                var params = { SrccId: $('#dlg_additem_SrccId').val(), ItemCode: $('#dlg_additem_ItemCode').val(), FavourFee: $('#dlg_additem_FavourFee').val(), RealityFee: $('#dlg_additem_RealityFee').val(), PrepayFee: $('#dlg_additem_PrepayFee').val() };
                requestAjax('../Controller.aspx?controller=RoomBrowseController&method=UpdateMeterSrccItemData', params, function(data) {
                    $('#dlg-additem').dialog('close');
                    $('#srccitemGird').datagrid('reload');
                    $.messager.progress('close');
                },function(){$.messager.progress('close');});
            }
        });
    } else {
        //1
        if (parseInt($('#dlg_additem_ThisCostFee').val()) == 0) {
            $.messager.alert('提示', "应收金额不能为零！", 'info', function() {
                $('#dlg_additem_ThisCostFee').focus().select();
            });
            return false;
        }

        //新增或修改其他型2项目操作
        $.messager.confirm('询问', '是否需要进行此操作？', function(r) {
            if (r) {
                $.messager.progress({
		            title:'修改费用',
		            msg:'正在提交数据，请等待...'
	            });
                var params = { HeadId: $('#dlg_additem_HeadId').val(), OwnerId: $('#dlg_additem_OwnerId').val(), OwnerName: $('#dlg_additem_OwnerName').val(), SrccId: $('#dlg_additem_SrccId').val(), ItemCode: $('#dlg_additem_ItemCode').val(), ItemName: $('#dlg_additem_ItemName').combobox('getText'), DosageNum: $('#dlg_additem_ThisNum').val(), Price: $('#dlg_additem_Price').val(), CostFee: $('#dlg_additem_ThisCostFee').val(), SrccItemMemo: $('#dlg_additem_memo').val() };
                requestAjax('../Controller.aspx?controller=RoomBrowseController&method=UpdateOtherSrccItemData', params, function(data) {
                    $('#dlg-additem').dialog('close');
                    $('#srccitemGird').datagrid('reload');
                    $.messager.progress('close');
                },function(){$.messager.progress('close');});
            }
        });
    }
}

//正式结算
function dlg_statitem_queding() {
    var rows = $('#srccitemGird').datagrid('getSelections');
    if (rows.length > 0) {
        if (formvalidate('dlg-cash') == false) return false;
        if (parseFloat($('#txtchange').val()) < 0) {
            $.messager.alert('提示', "输入的现金不足！", 'info', function() {
                $('#txtcash').focus().select();
            });
            return false;
        }
        $.messager.confirm('询问', '是否进行收银处理？', function(r) {
            if (r) {
                $.messager.progress({
		            title:'收银',
		            msg:'正在收银中，请等待...'
	            });
                var arr = [];
                for (var i in rows) arr.push(rows[i].HeadId + '#' + rows[i].SrccId + '#' + rows[i].ItemCode + '#' + rows[i].ItemType + '#' + rows[i].ThisCostFee + '#' + rows[i].ThisLateFee);
                requestAjax('../Controller.aspx?controller=RoomBrowseController&method=ProcessChargeMoney', { data: arr.join(',') }, function(data) {
                    $('#dlg-cash').dialog('close');
                    $.messager.progress('close');
                },function(){$.messager.progress('close');});
            }
        });
    }
}
